import Constants from '../constants'
import {messageExecute} from "@/modules/im/command/base";
import {chatListeners,contactListeners,rtcListeners} from "@/modules/im/command/listeners";
import {decodeChatMessage} from "@/modules/im/codec/decoder";

/**
 * 处理服务端发过来的消息（格式为String），转为Object，并使用相应命令处理消息
 * @param jsonStr
 */
export const handleMessage = (jsonStr) => {
    let msg = JSON.parse(jsonStr)
    let messageType = msg[Constants.PROPERTY_TYPE]
    if(messageType == null) {
        return
    }
    let content = msg[Constants.PROPERTY_CONTENT]
    switch (messageType) {
        case Constants.TYPE_CHAT:
            messageExecute(decodeChatMessage(content),chatListeners)
            break
        case Constants.TYPE_CONTACT:
            messageExecute(content,contactListeners)
            break
        case Constants.TYPE_RTC_CONNECT:
            messageExecute(content,rtcListeners)
            break
        default:
            break
    }
}
